package com.phs.main.security.jwt;

import com.alibaba.fastjson.JSON;
import com.phs.main.common.constant.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
@Slf4j
public class JWTFilter extends BasicHttpAuthenticationFilter {

	// 登录标识
	private static final String LOGIN_SIGN = "Authorization";

	/**
	 * 对跨域提供支持
	 *
	 * @param request  请求
	 * @param response 响应
	 * @return 允许值
	 * @throws Exception e
	 */
	@Override
	protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin"));
		httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
		httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers"));
		// 跨域时会首先发送一个option请求，这里我们给option请求直接返回正常状态
		if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
			httpServletResponse.setStatus(HttpStatus.OK.value());
			return false;
		}
		return super.preHandle(request, response);
	}

	/**
	 * 认证之前执行该方法
	 *
	 * @param request     请求
	 * @param response    响应
	 * @param mappedValue map
	 * @return 通过？
	 */
	@Override
	protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
		Subject subject = SecurityUtils.getSubject();
		return null != subject && subject.isAuthenticated();
	}

	/**
	 * 检测用户是否登录
	 * 检测header里面是否包含Authorization字段即可
	 *
	 * @param request  请求
	 * @param response 响应
	 * @return 登录？
	 */
	@Override
	protected boolean isLoginAttempt(ServletRequest request, ServletResponse response) {
		HttpServletRequest req = (HttpServletRequest) request;

		String authorization = req.getHeader(LOGIN_SIGN);

		return authorization != null;

	}

	@Override
	protected boolean executeLogin(ServletRequest request, ServletResponse response) {
		HttpServletRequest req = (HttpServletRequest) request;
		String header = req.getHeader(LOGIN_SIGN);

		JWTToken token = new JWTToken(header);

		// 提交给realm进行登入，如果错误会抛出异常并被捕获
		getSubject(request, response).login(token);
		// 如果没有抛出异常则代表登入成功，返回true
		return true;
	}

	/**
	 * 认证未通过执行该方法
	 *
	 * @param request  请求
	 * @param response 响应
	 * @return 通过？
	 */
	@Override
	protected boolean onAccessDenied(ServletRequest request, ServletResponse response) {
		//完成token登入
		//1.检查请求头中是否含有token
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		String token = httpServletRequest.getHeader(LOGIN_SIGN);
		//2. 如果客户端没有携带token，拦下请求
		if (null == token || "".equals(token)) {
			responseTokenError(response, "Token无效，您无权访问该接口");
			return false;
		}
		//3. 如果有，对进行进行token验证
		JWTToken jwtToken = new JWTToken(token);
		try {
			if (token.split("\\.").length < 3) {
				responseTokenError(response, "token 不对劲，还是没有token");
				return false;
			}
			SecurityUtils.getSubject().login(jwtToken);
		} catch (AuthenticationException e) {
			log.error(e.getMessage());
			responseTokenError(response, e.getMessage());
			return false;
		}

		return true;
	}

	/**
	 * 无需转发，直接返回Response信息 Token认证错误
	 */
	private void responseTokenError(ServletResponse response, String msg) {
		HttpServletResponse httpServletResponse = WebUtils.toHttp(response);
		httpServletResponse.setStatus(HttpStatus.OK.value());
		httpServletResponse.setCharacterEncoding("UTF-8");
		httpServletResponse.setContentType("application/json; charset=utf-8");
		try (PrintWriter out = httpServletResponse.getWriter()) {
			String data = JSON.toJSONString(AjaxResult.error(msg));
			out.append(data);
		} catch (IOException e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
	}
}

